package edu.gatech.ealf.ettplaf;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JComponent;

public class ToolTipComponent extends JComponent {

    private static final long serialVersionUID = 1L;
    private String text;
    public ToolTipComponent(){
    }
    
    public void setText(String text){
        this.text = text;
    }
    
    public String getText(){
    	return text;
    }
    
    @Override
    public void paintComponent(Graphics g){
    	
        g.setFont(getFont());
    	g.setColor(getBackground());
    	g.fillRoundRect(0,0, getWidth(), getHeight(), 10, 10);
    	g.setColor(getForeground());
    	paintString(g);
    }

	private void paintString(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING , RenderingHints.VALUE_ANTIALIAS_ON);
		
		FontMetrics fm = g.getFontMetrics();
    	int fontHeight = fm.getHeight();
    	String words[] = text.split(" ");
    	int width = getWidth();
    	int height = getHeight();
    	int curX = 3;
    	int curY = fontHeight;
    	StringBuffer line = new StringBuffer();
    	for(String word : words){
    		int wordWidth = fm.stringWidth(word + " ");
    		if(curX + wordWidth < width){
    			line.append(word).append(" ");
    			curX += wordWidth;
    		} else {
    			g.drawString(line.toString(), 3, curY);
    			line = new StringBuffer();
    			curX = 3;
    			curY += fontHeight;
    			if(curY > height){
    				height = height + fontHeight;
    				Dimension newDim = new Dimension(width, height);
    				//setMinimumSize(newDim);
    				setPreferredSize(newDim);
    				setSize(newDim);
    			}
    			if(curX + wordWidth < width){
    				line.append(word).append(" ");
        			curX += wordWidth;
    			} else {
    				width = width + wordWidth;
    				Dimension newDim = new Dimension(width, height);
    				//setMinimumSize(newDim);
    				setPreferredSize(newDim);
    				setSize(newDim);
    				line.append(word).append(" ");
        			curX += wordWidth;
    			}
    		}
    	}

		g.drawString(line.toString(), 3, curY);
	}
    
}
